function sfh = fif_WriteSFH(hfile, sfhfile)
% FIF::WriteSFH  - write SFH file
%
% FORMAT:       sfh = fif.CreateCTC(sfhfile)
%
% Input fields:
%
%       sfhfile     name of SFH output file
%
% Output fields:
%
%       sfh         the created object

% Version:  v0.7b
% Build:    7083009
% Date:     Aug-30 2007, 9:31 AM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 2 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'fif') || ...
   ~ischar(sfhfile) || ...
    isempty(sfhfile)
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
sfhfile = sfhfile(:)';

% get object
bc = bvqxfile_getcont(hfile.L);

% create CTC in memory
sfh = BVQXfile('new:sfh');
sfc = bvqxfile_getcont(sfh.L);

% get fif shortcut
fif = bc.FIFStructure;

% any dig points ?
iel = any(fif.Lookup == 213);
if ~iel
    BVQXfile(0, 'clearobj', sfh.L);
    error( ...
        'BVQXfile:BadInputFile', ...
        'FIF file does not contain DigPoint tags.' ...
    );
end

% get all DigPoints
dps = fif_Value(hfile, 'DigPoint');

% parse dig points
cfid = zeros(1, 0);
chpi = zeros(1, 0);
cext = zeros(1, 0);
for pc = 1:numel(dps)
    switch lower(dps(pc).Kind)
        case {'headfiducial'}
            if ~any(dps(pc).Ident == [1, 2, 3])
                BVQXfile(0, 'clearobj', sfh.L);
                error( ...
                    'BVQXfile:UnexpectedToken', ...
                    'Only three major fiducials supported.' ...
                );
            end
            cfid(dps(pc).Ident) = pc;
        case {'hpipoint'}
            chpi(dps(pc).Ident) = pc;
        case {'extrapoint'}
            cext(dps(pc).Ident) = pc;
        otherwise
            warning( ...
                'BVQXfile:UnexpectedToken', ...
                'Unknown kind of DigPoint %d for point %d.', ...
                dps(pc).Kind, pc ...
            );
    end
end
if numel(cfid) ~= 3 || ...
    any([cfid, chpi, cext] == 0)
	BVQXfile(0, 'clearobj', sfh.L);
    error( ...
        'BVQXfile:InvalidFile', ...
        'Invalid organization of fiducial points.' ...
    );
end

% build fiducial list
fstr = struct;
fstr.Fid_T9  = [1000 * dps(cfid(1)).Coord, 3, 255, 128, 255];
fstr.Fid_Nz  = [1000 * dps(cfid(2)).Coord, 3, 255, 128, 255];
fstr.Fid_T10 = [1000 * dps(cfid(3)).Coord, 3, 255, 128, 255];
for fc = 1:numel(chpi)
    fstr.(sprintf('HPI_%d', fc + 3)) = ...
        [1000 * dps(chpi(fc)).Coord, 2, 0, 255, 0];
end
for fc = 1:numel(cext)
    fstr.(sprintf('Extra_%d', fc + 3 + numel(chpi))) = ...
        [1000 * dps(cext(fc)).Coord, 2, 0, 255, 255];
end

% set number of points and save
sfc.Fiducials = fstr;
sfc.NrOfPoints = numel(fieldnames(fstr));
bvqxfile_setcont(sfh.L, sfc);
try
    aft_SaveAs(sfh, sfhfile);
catch
    warning( ...
        'BVQXfile:SaveFailed', ...
        'Saving SFH file failed: %s.', ...
        lasterr ...
    );
end
